import { getAlarmWayList, getAlarmTypeList } from '@/api/alarm/alarm'
import { BasicColumn, FormSchema } from '@/components/Table'
import { h } from 'vue'
import { Tag } from 'ant-design-vue'

export const columns: BasicColumn[] = [
  {
    title: '告警规则名称',
    dataIndex: 'alarmRuleName',
    width: 100,
  },
  {
    title: '适用范围',
    dataIndex: 'alarmRange',
    width: 100,
    customRender: ({ record }) => {
      return record?.alarmRangeList
        ? record?.alarmRangeList
            .map((item) => `${item?.deviceTypeName}，${item?.alarmTypeName}`)
            .join('；')
        : '-'
    },
  },
  {
    title: '通知角色',
    dataIndex: 'alarmRole',
    width: 120,
    customRender: ({ record }) => {
      return record?.alarmRuleRoleVOList
        ? record?.alarmRuleRoleVOList
            .map((item) => item?.roleName)
            .join('；')
        : '-'
    },
  },
  {
    title: '状态',
    dataIndex: 'status',
    width: 80,
    customRender: ({ record }) => {
      const status = record.status === 1
      const color = status ? 'green' : 'red'
      const text = status ? '启用' : '停用'
      return h(Tag, { color: color }, () => text)
    },
  },
  {
    title: '更新人',
    dataIndex: 'updateUserName',
    width: 120,
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
    width: 120,
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    field: 'name',
    label: '告警名称',
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    field: 'alarmTypeId',
    label: '告警类型',
    component: 'ApiSelect',
    colProps: { span: 6 },
    componentProps: {
      api: getAlarmTypeList,
      labelField: 'name',
      valueField: 'id',
    },
  },
  {
    field: 'status',
    label: '状态',
    component: 'Select',
    colProps: { span: 6 },
    componentProps: {
      options: [
        {
          label: '启用',
          value: 1,
        },
        {
          label: '停用',
          value: 0,
        },
      ],
    },
  },
]

export const accountFormSchema: FormSchema[] = [
  {
    field: 'alarmRuleName',
    label: '告警名称',
    component: 'Input',
    rules: [
      {
        required: true,
        message: '请输入告警名称',
      },
      // {
      //   trigger: 'blur',
      //   validator(_, value) {
      //     return new Promise((resolve, reject) => {
      //       if (!value) return resolve();
      //       isAccountExist(value)
      //         .then(resolve)
      //         .catch((err) => {
      //           reject(err.message || '验证失败');
      //         });
      //     });
      //   },
      // },
    ],
  },
  {
    label: '告警方式',
    field: 'alarmType',
    component: 'ApiSelect',
    componentProps: {
      api: getAlarmWayList,
      labelField: 'label',
      valueField: 'value',
    },
    required: true,
  },
  {
    label: '通知角色',
    field: 'alarmRuleRoleVOList',
    component: 'Select',
    componentProps: {
      mode: 'multiple',
      labelInValue: true,
      fieldNames: {
        label: 'roleName',
        value: 'roleId',
      },
    },
    required: true,
  },
  {
    field: 'alarmRangeList',
    label: '适用范围',
    slot: 'alarmRange',
    required: true,
    defaultValue: [
      {
        deviceTypeId: undefined,
        alarmTypeId: undefined,
      },
    ],
    rules: [
      {
        required: true,
        message: '请配置适用范围',
      },
      {
        validator(_, value) {
          return new Promise((resolve, reject) => {
            if (!value.length) {
              reject('请配置适用范围')
            } else if (value.filter((item) => !item.deviceTypeId || !item.alarmTypeId).length) {
              reject('请完善适用范围')
            } else {
              return resolve()
            }
          })
        },
      },
    ],
  },
]
